package com.zf.developaccumulate.controller;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;

/**
 * 代码调用多文件上传接口，以二进制形式上传文件
 */
@Slf4j
@RestController
@RequestMapping("/fileBinary")
public class FileBinaryUploadController {

    /**
     * 上传多个文件，以文件的二进制作为请求体
     */
    @PostMapping(value = "/uploadMultiFile")
    public String upload(@RequestBody FileUploadEntity formEntity) throws IOException {
        log.info("username: {}, age: {}", formEntity.getUsername(), formEntity.getAge());
        List<FilePojo> attachFileList = formEntity.getAttachFileList();
        if (CollectionUtils.isNotEmpty(attachFileList)) {
            for (FilePojo file : attachFileList) {
                // 文件名
                String originalFilename = file.getFilename();
                log.info("originalFilename: {}", originalFilename);
                // 获取文件二进制
                byte[] content = file.getContent();
                if (content == null || content.length == 0) {
                    continue;
                }
                // 将读取的内容写入一个新的文件中
                FileOutputStream outputStream = new FileOutputStream("D:\\out-" + originalFilename);
                FileCopyUtils.copy(content, outputStream);
            }
        }
        return "success";
    }

    /**
     * 代码调用上传表单接口
     */
    @PostMapping("/callUploadMultiFile")
    public String callUploadMultiFile() throws IOException {
        // 创建请求头
        HttpHeaders headers = new HttpHeaders();
        // 设置 Content-Type 为 "application/json"
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 二进制源，可以从文件、网络流中读取数据，更灵活，推荐使用这种方式
        byte[] file1Bytes = Files.readAllBytes(Path.of("D:\\file1.txt"));
        byte[] file2Bytes = Files.readAllBytes(Path.of("D:\\file2.txt"));

        // 创建一个文件对象
        FilePojo file1Resource = new FilePojo("file111.txt", file1Bytes);
        FilePojo file2Resource = new FilePojo("file222.txt", file2Bytes);

        // 创建请求体，并添加内容
        FileUploadEntity requestBody = new FileUploadEntity();
        requestBody.setUsername("王五");
        requestBody.setAge(23);
        requestBody.setAttachFileList(Arrays.asList(file1Resource, file2Resource));

        // 创建HttpEntity，组合请求体和请求头
        HttpEntity<Object> requestEntity = new HttpEntity<>(requestBody, headers);

        // 创建 RestTemplate
        RestTemplate restTemplate = new RestTemplate();
        // 发送请求
        ResponseEntity<String> response = restTemplate.postForEntity("http://localhost:8109/fileBinary/uploadMultiFile", requestEntity, String.class);
        return response.getBody();
    }

    /**
     * 参数
     */
    @Getter
    @Setter
    public static class FileUploadEntity {
        private String username;
        private Integer age;
        private List<FilePojo> attachFileList;
    }

    /**
     * 文件对象
     */
    @Getter
    @Setter
    public static class FilePojo {
        private String filename;
        private byte[] content;

        public FilePojo() {
        }

        public FilePojo(String filename, byte[] content) {
            this.filename = filename;
            this.content = content;
        }
    }


}
